// @Author YaoYa
// @Date 2024-10-18 18:02:00
// @Desc
package pay

import (
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/smartwalle/alipay/v3"
	"go.uber.org/zap"
	"mxshop-api/order-web/global"
	"mxshop-api/order-web/proto"
	"net/http"
)

// 支付成功后回调地址，完成状态变更
func Notify(ctx *gin.Context) {
	//支付宝回调通知
	client, err := alipay.New(global.ServerConfig.AliPayInfo.AppID, global.ServerConfig.AliPayInfo.PrivateKey, false)
	if err != nil {
		zap.S().Errorw("实例化支付宝失败")
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg": err.Error(),
		})
		return
	}
	err = client.LoadAliPayPublicKey((global.ServerConfig.AliPayInfo.AliPublicKey))
	if err != nil {
		zap.S().Errorw("加载支付宝的公钥失败")
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg": err.Error(),
		})
		return
	}
	//获取交易通知并回调
	noti, err := client.GetTradeNotification(ctx.Request)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{})
		return
	}

	_, err = global.OrderSrvClient.UpdateOrderStatus(context.Background(), &proto.OrderStatus{
		OrderSn: noti.OutTradeNo,
		Status:  string(noti.TradeStatus),
	})
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{})
		return
	}
	ctx.String(http.StatusOK, "success")
}

func generateAliPayUrl() {
	appId := "9021000141638578"
	privateKey := "MIIEogIBAAKCAQEAlA6N4GkLPaOhe6eiP02o2XrZqj6gdwXM2TXt0UUGVyiDX3rPa+56S9jQeT7x7TAKXV6MdeUn1TsV9yPw0nfh6zYVaCKzDhYKOtzzfWhJwqsOVjmYKGEbrc0qLeU6QM/BDasjZkwVKoqOElXYCGRMNtpXpcudYKY5YRn3MR+OwjZ1nEJytJpWLsF56YvGCEe1hDXvZ9Nh8Y3reDwhN+ZCemlL/O777vkFDFAta+vy/nUPQuDxelwQmB0EQ/8SiHew9HcA+pv7UQIa9Xm+8yI38x2f82VSL7cUW3wMcWxBdx0DnkQm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ln8DTjqDQQCzSELNd5duZDzbbBBRqjNJ5n1IamgYgprn/x+frdENuDwjWwl9zBI1Vn/Y3d42U9d8WZ3gMZxD/8UCEMRIzBUYG6abZTZCJnNqw/6tM5x3PVvOe5kZhsd+fkB4AZfgFFZV8tDC5nHyHHF7YOcOpfZpF72Yv9Seik=" // 必须，上一步中使用 RSA签名验签工具 生成的私钥
	//回调接口要用到阿里公钥
	//aliPublicKey := "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnDZtuYVdw9MWg4Y3B2v7pypuURZTTeWtUcqg6YXL8PsHzWGpRDsZhHeexDtnWCRbeuGl5ojPXXXeSGzwxS7AfmLC0nC2JYGeJJ5GWLT0jb/XOVfmEvttHEHIlG8oYkXIVYXyHKzAsq0gzoKGB3Y6aCkBRZKjfpnoWHzzOT+Lllirk31dXr6y9AveMRfjo0tU4pVsoyCfWQPL5PWaxVCTE9CBS4hWrfy4DQpNbQvzFW2P9E5G4K70zuEBEZqld5QzkQrjYmEoplkpefLERs3MXoAD7yASfvR2Jaq8WB+o6IHMMtyQ+4hhEShibb9HKWTjdiZFNcmRa/ZbzCdF1kbeVwIDAQAB"
	var client, err = alipay.New(appId, privateKey, false)
	if err != nil {
		fmt.Println(err)
		return
	}

	// 加载应用公钥证书
	if err = client.LoadAppCertPublicKeyFromFile("appCertPublicKey_2017011104995404.crt"); err != nil {
		// 错误处理
	}

	//// 加载支付宝根证书
	//if err = client.LoadAliPayRootCertFromFile("alipayRootCert.crt"); err != nil {
	//	// 错误处理
	//}
	//
	//// 加载支付宝公钥证书
	//if err = client.LoadAlipayCertPublicKeyFromFile("alipayCertPublicKey_RSA2.crt"); err != nil {
	//	// 错误处理
	//}

	//// 加载内容密钥，可选
	//if err = client.SetEncryptKey("FtVd5SgrsUzYQRAPBmejHQ=="); err != nil {
	//	// 错误处理
	//}

	var p = alipay.TradeWapPay{}
	p.NotifyURL = "http://www.baidu.com" //异步通知地址 ->授权回调地址
	p.ReturnURL = "http://www.baidu.com" //回调地址 ->支付完成后的回调地址
	p.Subject = "沐雪生鲜"
	p.OutTradeNo = "123333"
	p.TotalAmount = "13.00"
	//p.ProductCode = "QUICK_WAP_WAY"
	//p.ProductCode = "FACE_TO_FACE_PAYMENT" //扫码支付
	p.ProductCode = "FAST_INSTANT_TRADE_RAY" //支付宝支付
	url, err := client.TradeWapPay(p)
	if err != nil {
		fmt.Println(err)
	}

	// 这个 payURL 即是用于打开支付宝支付页面的 URL，可将输出的内容复制，到浏览器中访问该 URL 即可打开支付页面。
	var payURL = url.String()
	fmt.Println(payURL)
}
